//
//  LocationModel.swift
//  LocationTrackerAdmin
//
//  Created by mac on 2025/2/25.
//  定位数据模型

import Vapor
import Fluent
import SQLKit
import FluentSQL

final class LocationModel: Model, Content {
    
    static var schema: String {
        return "locations"
    }
    
    @ID(custom: LocationModel.id)
    var id: String? //定位id
    
    @Field(key: LocationModel.deviceID)
    var deviceID: String
    
    @Field(key: LocationModel.createDate)
    var createDate: Date
    
    @Field(key: LocationModel.latitude)
    var latitude: Double?
    
    @Field(key: LocationModel.longitude)
    var longitude: Double?
    
    @Field(key: LocationModel.altitude)
    var altitude: Double?
    
    @Field(key: LocationModel.accuracy)
    var accuracy: Double?
    
    @Field(key: LocationModel.altitudeAccuracy)
    var altitudeAccuracy: Double?
    
    @Field(key: LocationModel.heading)
    var heading: Double?
    
    @Field(key: LocationModel.speed)
    var speed: Double?
    
    @Field(key: LocationModel.detail)
    var detail: String?
    
    init() {
        
    }
    
    init(id: String? = nil, deviceID: String, createDate: Date, latitude: Double? = nil, longitude: Double? = nil, altitude: Double? = nil, accuracy: Double? = nil, altitudeAccuracy: Double? = nil, heading: Double? = nil, speed: Double? = nil, detail: String? = nil) {
        self.id = id
        self.deviceID = deviceID
        self.createDate = createDate
        self.latitude = latitude
        self.longitude = longitude
        self.altitude = altitude
        self.accuracy = accuracy
        self.altitudeAccuracy = altitudeAccuracy
        self.heading = heading
        self.speed = speed
        self.detail = detail
    }
}

struct LocationModelMigration: AsyncMigration {
    func prepare(on database: any Database) async throws {
        try await database.schema(LocationModel.schema)
            .field(LocationModel.id, .string, .identifier(auto: false), .custom("COMMENT '定位ID'"))
            .field(LocationModel.deviceID, .string, .required, .custom("COMMENT '设备ID'"))
            .field(LocationModel.createDate, .custom("datetime(0)"), .required, .sql(.default(SQLFunction("now"))), .custom("COMMENT '创建日期'"))
            .field(LocationModel.latitude, .double, .custom("COMMENT '纬度'"))
            .field(LocationModel.longitude, .double, .custom("COMMENT '经度'"))
            .field(LocationModel.altitude, .double, .custom("COMMENT '海拔'"))
            .field(LocationModel.accuracy, .double, .custom("COMMENT '精度'"))
            .field(LocationModel.altitudeAccuracy, .double, .custom("COMMENT '海拔精度'"))
            .field(LocationModel.heading, .double, .custom("COMMENT '方向'"))
            .field(LocationModel.speed, .double, .custom("COMMENT '速度'"))
            .field(LocationModel.detail, .string, .custom("COMMENT '地址详情'"))
            .create()
    }
    
    func revert(on database: any Database) async throws {
        try await database.schema(LocationModel.schema)
            .delete()
    }
}

extension LocationModel {
    static let id: FieldKey = FieldKey(stringLiteral: "location_id")
    static let deviceID: FieldKey = FieldKey(stringLiteral: "location_device_id")
    static let createDate: FieldKey = FieldKey(stringLiteral: "location_create_date")
    static let latitude: FieldKey = FieldKey(stringLiteral: "location_latitude")
    static let longitude: FieldKey = FieldKey(stringLiteral: "location_longitude")
    static let altitude: FieldKey = FieldKey(stringLiteral: "location_altitude")
    static let accuracy: FieldKey = FieldKey(stringLiteral: "location_accuracy")
    static let altitudeAccuracy: FieldKey = FieldKey(stringLiteral: "location_altitude_accuracy")
    static let heading: FieldKey = FieldKey(stringLiteral: "location_heading")
    static let speed: FieldKey = FieldKey(stringLiteral: "location_speed")
    static let detail: FieldKey = FieldKey(stringLiteral: "location_detail")
}
